package com.logisticsmanagement.services;



import java.util.ArrayList;
import java.util.List;

import com.logisticsmanagement.dao.DeliveryStationDao;
import com.logisticsmanagement.dao.UserDao;
import com.logisticsmanagement.domain.User;
import com.logisticsmanagement.utils.GlobelValue;
import com.logisticsmanagement.vo.BaseResponse;
import com.logisticsmanagement.vo.VoUser;


public class UserService{
	
	private UserDao userDao=new UserDao();
	private DeliveryStationDao deliveryStationDao = new DeliveryStationDao();
	
	public BaseResponse<VoUser> login(String userName,String password){
		
		User user = new User();
		user.setUserID(userName);
		user.setPassword(password);
		user.setIsDelete(false);
		
		List<User> queryuser = userDao.selectPage(user, 1, 1, null);
		if(queryuser.size() ==0){
			//login failed ,user name or password error
			return new BaseResponse<VoUser>(GlobelValue.RESULT_LOGIN_FAILED, GlobelValue.RESULT_LOGIN_FAILED_DESCRIPTON);
		}
		user = queryuser.get(0);
		
		VoUser voUser = User2VoUser(user);
		
		ArrayList<VoUser> list = new ArrayList<VoUser>();
		
		
		list.add(voUser);
		return new BaseResponse<VoUser>(GlobelValue.RESULT_OK, GlobelValue.RESULT_OK_DESCRIPTON,list);
	}
	
	public BaseResponse<VoUser> register(VoUser inputUser){
		
		if(inputUser.getUserID() == null || inputUser.getDeliveryStationID() <0
				|| inputUser.getRole() == null || inputUser.getAddress() == null 
				|| inputUser.getMobilePhone() == null || inputUser.getPassword() == null){
			return new BaseResponse<VoUser>(GlobelValue.RESULT_PAREM_NOT_COMPLETE, GlobelValue.RESULT_PAREM_NOT_COMPLETE_DESCRIPTION);
		}

		if(userDao.selectByID(inputUser.getUserID()) != null){
			return new BaseResponse<VoUser>(GlobelValue.RESULT_REGISTER_USERNAME_DUPLICATE, GlobelValue.RESULT_REGISTER_USERNAME_DUPLICATE_DESCRIPTION);
		}
		
		if(inputUser.getDisplayName() == null){
			inputUser.setDisplayName(inputUser.getUserID());
		}
		
		if(!userDao.add(VoUser2User(inputUser)) ){
			return new BaseResponse<VoUser>(GlobelValue.RESULT_SERVER_ERROR, GlobelValue.RESULT_SERVER_ERROR_DESCRIPTION);
		}
		
		
		return new BaseResponse<VoUser>(GlobelValue.RESULT_OK, GlobelValue.RESULT_OK_DESCRIPTON);
	}
	

	public BaseResponse<VoUser> update(VoUser inputUser){
		if(inputUser.getUserID() == null || inputUser.getDeliveryStationID() <0
				|| inputUser.getRole() == null || inputUser.getAddress() == null 
				|| inputUser.getMobilePhone() == null || inputUser.getPassword() == null){
			return new BaseResponse<VoUser>(GlobelValue.RESULT_PAREM_NOT_COMPLETE, GlobelValue.RESULT_PAREM_NOT_COMPLETE_DESCRIPTION);
		}
		if(inputUser.getDisplayName() == null){
			inputUser.setDisplayName(inputUser.getUserID());
		}
		DeliveryStationDao deliveryStationDao = new DeliveryStationDao();
		if(deliveryStationDao.selectById(inputUser.getDeliveryStationID()) == null){
			return new BaseResponse<VoUser>(GlobelValue.RESULT_FOREIN_KEY_CONSTRAINT_FAILED, GlobelValue.RESULT_FOREIN_KEY_CONSTRAINT_FAILED_DESCRIPTON);
		}
		
		
		if(!userDao.update(VoUser2User(inputUser))){
			return new BaseResponse<VoUser>(GlobelValue.RESULT_SERVER_ERROR, GlobelValue.RESULT_SERVER_ERROR_DESCRIPTION);
		}
		ArrayList<VoUser> list = new ArrayList<VoUser>();
		list.add(inputUser);
		return new BaseResponse<VoUser>(GlobelValue.RESULT_OK, GlobelValue.RESULT_OK_DESCRIPTON,list);
	}

	public BaseResponse<VoUser> getList(VoUser requirement,VoUser opreatUser,int pageNow,int pageSize){
		
		List<User> list= null;
		if(opreatUser.getUserID() == null){
			return new BaseResponse<VoUser>(GlobelValue.RESULT_PAREM_NOT_COMPLETE, GlobelValue.RESULT_PAREM_NOT_COMPLETE_DESCRIPTION);
		}
		User realopretor = userDao.selectByID(opreatUser.getUserID());
		if(realopretor == null){
			return new BaseResponse<VoUser>(GlobelValue.RESULT_LOGIN_FAILED, GlobelValue.RESULT_LOGIN_FAILED_DESCRIPTON);
		}
		
		if(realopretor.getRole().equals(GlobelValue.NAME_SYSTEM_ADMIN)){//system administritor,all opreate is allowed
			list = userDao.selectPage(VoUser2User(requirement), pageNow, pageSize, null);
		}else if(realopretor.getRole().equals(GlobelValue.NAME_PROVINCE_ADMIN)){
			int cityID = deliveryStationDao.selectById(realopretor.getDeliveryStationID()).getCityID();
			list = userDao.selectPageByCityID(cityID, pageNow, pageSize);
		}else if(realopretor.getRole().equals(GlobelValue.NAME_DELIVERY_STATION_ADMIN)){
			requirement.setDeliveryStationID(realopretor.getDeliveryStationID());
			list = userDao.selectPage(VoUser2User(requirement), pageNow, pageSize, null);
		}else{
			return new BaseResponse<VoUser>(GlobelValue.RESULT_PERMISSION_DENY, GlobelValue.RESULT_PERMISSION_DENY_DESCRIPTON);
		}
		ArrayList<VoUser> listVoUser = new ArrayList<VoUser>();
		
		for(User user:list){
			listVoUser.add(User2VoUser(user));
		}
		return new BaseResponse<VoUser>(GlobelValue.RESULT_OK, GlobelValue.RESULT_OK_DESCRIPTON,listVoUser);
	}
	
	public BaseResponse<VoUser> getUserInfo(String userID){
		VoUser voUser = User2VoUser(userDao.selectByID(userID));
		
		if(voUser == null){
			return new BaseResponse<VoUser>(GlobelValue.RESULT_LOGIN_FAILED, GlobelValue.RESULT_LOGIN_FAILED_DESCRIPTON);
		}
		
		ArrayList<VoUser> list = new ArrayList<VoUser>();
		list.add(voUser);
		return new BaseResponse<VoUser>(GlobelValue.RESULT_OK, GlobelValue.RESULT_OK_DESCRIPTON,list);
	}
	private User VoUser2User(VoUser inputUser){
		User user = new User();
		user.setAddress(inputUser.getAddress());
		user.setDeliveryStationID(inputUser.getDeliveryStationID());
		user.setDisplayName(inputUser.getDisplayName());
		user.setGender(inputUser.getGender());
		user.setIsDelete(false);
		user.setMobilePhone(inputUser.getMobilePhone());
		user.setPassword(inputUser.getPassword());
		user.setPrepareDelete(false);
		user.setRole(inputUser.getRole());
		user.setUserID(inputUser.getUserID());
		return user;
	}
	
	private VoUser User2VoUser(User user){
		VoUser voUser = new VoUser();
		voUser.setAddress(user.getAddress());
		voUser.setDeliveryStationID(user.getDeliveryStationID());
		voUser.setDisplayName(user.getDisplayName());
		voUser.setGender(user.getGender());
		voUser.setMobilePhone(user.getMobilePhone());
		voUser.setRole(user.getRole());
		voUser.setUserID(user.getUserID());
		voUser.setDeliveryStationName(deliveryStationDao.selectById(user.getDeliveryStationID()).getStationName());
		
		return voUser;
	}
}
